//点击label修改状态
const changeIsRequired = function (todoList,id) {
    const newArrList = todoList.map(todo=>{
        if(todo.id===id){
            todo.isRequired = !todo.isRequired
        }
        return todo
    })
    localStorage.setItem('todoList',JSON.stringify(newArrList))
    return newArrList
}

//添加todo
const addTodo = function (todoList,title) {
    const newArrList = [{id:Math.random().toString(36).slice(2),name:title,isRequired:false},...todoList]
    localStorage.setItem('todoList',JSON.stringify(newArrList))
    return newArrList
}

//删除todo
const deleteTodo = function (todoList,id) {
    const newArrList = todoList.filter(todo=>todo.id!==id)
    localStorage.setItem('todoList',JSON.stringify(newArrList))
    return newArrList
}

//修改所有todo的状态
const allIsRequiredChange = function (todoList,flag) {
    const newArrList = todoList.map(todo=>{
        todo.isRequired = flag
        return todo
    })
    localStorage.setItem('todoList',JSON.stringify(newArrList))
    return newArrList
}

//删除已选中的todo
const deleteSelected = function (todoList) {
    const newArrList = todoList.filter(todo=>!todo.isRequired)
    localStorage.setItem('todoList',JSON.stringify(newArrList))
    return newArrList
}

export {changeIsRequired,addTodo,deleteTodo,allIsRequiredChange,deleteSelected}